package logic

import (
	"context"
	"fmt"
	"user_srv/models"
	"user_srv/pkg"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type AuthLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthLogic {
	return &AuthLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AuthLogic) Auth(in *user.AuthRealnameRequest) (*user.AuthRealnameResponse, error) {
	// todo: add your logic here and delete this line
	var auth models.Userldentity

	var err error

	auth.UserId = in.UserId

	if err = auth.GetUserLdentidyByuserId(); err != nil {
		logx.Error("实名验证失败1")
		return nil, err
	}

	if auth.ID > 0 {
		logx.Error("用户已实名")
		return nil, fmt.Errorf("用户已实名")
	}

	if !pkg.Auth(in.IdCard, in.RealName) {
		logx.Error("实名验证失败,第三方接口失败")
		return nil, fmt.Errorf("第三方调用失败")
	}

	auth.RealName = in.RealName
	auth.IdCard = in.IdCard

	if err = auth.Create(); err != nil {
		logx.Error("实名验证失败2")
		return nil, err
	}

	return &user.AuthRealnameResponse{}, nil
}
